package com.ccb.sc.order.controller;

import com.ccb.sc.auth.TokenAuth;
import com.ccb.sc.auth.TokenModel;
import com.ccb.sc.order.service.IAppOrderReturnService;
import com.ccb.sc.common.bind.annotation.CurrentUser;
import com.ccb.sc.common.bind.page.PageParams;
import com.ccb.sc.common.bind.page.PageVo;
import com.ccb.sc.common.bind.response.ResBody;
import com.ccb.sc.common.modules.common.vo.QueryTrackResp;
import com.ccb.sc.common.modules.order.convert.OrderReturnConvert;
import com.ccb.sc.common.modules.order.entity.OrderReturn;
import com.ccb.sc.common.modules.order.service.impl.OrderRefundService;
import com.ccb.sc.common.modules.order.vo.OrderRefundVo;
import com.ccb.sc.common.modules.order.vo.OrderReturnCreateVo;
import com.ccb.sc.common.modules.order.vo.OrderReturnListVo;
import com.ccb.sc.common.modules.order.vo.OrderReturnSettingsVo;
import com.ccb.sc.common.modules.setting.service.ISettingsService;
import com.ccb.sc.common.modules.setting.vo.OrderSettingsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Objects;

/**
 * @author ken
 * @version 1.0
 * @date 2020-10-14
 */
@RestController
@TokenAuth
@RequestMapping("/app/order")
@Api(value = "退货退款", tags = "退货退款")
public class OrderReturnController {

    private IAppOrderReturnService orderReturnService;

    private ISettingsService settingsService;

    private OrderRefundService orderRefundService;

    @GetMapping("/return/list")
    @ApiOperation(value = "退货退款列表", response = OrderReturnListVo.class, responseContainer = "list")
    public ResBody<?> list(@CurrentUser @ApiIgnore TokenModel token, PageParams queryParam) {
        PageVo<OrderReturnListVo> page = this.orderReturnService.list(token.getId(), queryParam);
        return ResBody.success(page);
    }

    @PostMapping("/return")
    @ApiOperation(value = "退货退款")
    public ResBody<?> returnGoods(@CurrentUser @ApiIgnore TokenModel token, @RequestBody @Validated OrderReturnCreateVo createVo){
        this.orderReturnService.returnGoods(token.getId(), createVo);
        return ResBody.success();
    }

    @GetMapping("/return/{orderReturnId}")
    @ApiOperation(value = "退换/售后详情", response = OrderReturnListVo.class)
    public ResBody<?> list(@CurrentUser @ApiIgnore TokenModel token, @PathVariable Long orderReturnId) {
        OrderReturnListVo detail = this.orderReturnService.detail(token.getId(), orderReturnId);
        return ResBody.success(detail);
    }

    @GetMapping("/refund/{orderReturnId}")
    @ApiOperation(value = "退款详情", response = OrderRefundVo.class)
    public ResBody<?> refund(@CurrentUser @ApiIgnore TokenModel token, @PathVariable Long orderReturnId) {
        OrderReturn orderReturn = this.orderReturnService.getById(orderReturnId);
        Assert.isTrue(orderReturn != null && Objects.equals(orderReturn.getMemberId(), token.getId()), "数据异常，退款详情不存在");
        OrderRefundVo orderRefundVo = this.orderRefundService.getByOrderReturnId(orderReturnId);
        if (orderRefundVo != null) {
            orderRefundVo.setGoodsName(orderReturn.getGoodsName());
            orderRefundVo.setGoodsSpec(orderReturn.getGoodsSpec());
            orderRefundVo.setPicture(orderReturn.getPicture());
            orderRefundVo.setQuantity(orderReturn.getQuantity());
        }
        return ResBody.success(orderRefundVo);
    }

    @GetMapping("/return/trace/{orderReturnId}")
    @ApiOperation(value = "退换订单物流信息", response = QueryTrackResp.class)
    public ResBody<?> trace(@CurrentUser @ApiIgnore TokenModel token, @PathVariable Long orderReturnId) {
        QueryTrackResp queryTrackResp = this.orderReturnService.orderTrace(orderReturnId);
        return ResBody.success(queryTrackResp);
    }

    @GetMapping("/return/setting")
    @ApiOperation(value = "退换货说明")
    public ResBody<?> setting() {
        OrderSettingsVo settings = this.settingsService.getSettings(OrderSettingsVo.class);
        OrderReturnSettingsVo settingsVo = OrderReturnConvert.INSTANCE.setting2Vo(settings);
        return ResBody.success(settingsVo);
    }


    @Autowired
    public void setOrderReturnService(IAppOrderReturnService orderReturnService) {
        this.orderReturnService = orderReturnService;
    }

    @Autowired
    public void setSettingsService(ISettingsService settingsService) {
        this.settingsService = settingsService;
    }

    @Autowired
    public void setOrderRefundService(OrderRefundService orderRefundService) {
        this.orderRefundService = orderRefundService;
    }
}
